<template>
  <div class='container'>
    <div class="lego1-wrap mv-banner" bx-name="mv/banner" bx-version="0.0.6" bx-guid="lego1" hasjs="false" hascss="true" bx-slot="slot-3" data-spm="21368908">
      <div class="mv-banner-comp lego1 mv-comp  img-loaded" style="margin-bottom: 0.24rem; background-image: url(//gw.alicdn.com/tfs/TB1D8V3y7L0gK0jSZFtXXXQCXXa-750-292.jpg_q75.jpg_.webp);"></div>
    </div> 
    <div class="tabview-tab" id="mx-13">
      <div class="xtab theme-loc-left slides-per-view-auto xtab-padding" data-slidesperview="auto">
        <div class="xtab-container swiper-container">
          <div class="xtab-wrap swiper-wrapper">
            <div :class="'xs-item swiper-slide flex-1 ' +  (tabIndex == key ?'xs-item-active':'xs-item')"  v-for="(item,key) in tabs" :key="key" @click="clickTab(key)">
              <div class="xtab-item-content">
                <div class="xtab-icon-title">
                  {{item.name}}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-for="(item,key) in tabs" :key="key" v-show="tabIndex==key">
      <Good v-for="(good,k) in item.list" :key="k" :info="good"></Good>
    </div>
    <p class='loading'>{{loadingTxt}}</p>
    <p class="c_fcopyright">© 2020 鱼米之乡  <a href="http://beian.miit.gov.cn">粤ICP备19089756号-1</a> </p>
  </div>
</template>

<script>
import Good from "../components/Good.vue";
import axios from "axios";
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  components: {
    Good
  },
  data() {
      return {
        loadingTxt: '加载中...',
        loading: false,
        keyword: '',
        tabIndex: 0,
        tabs: [
          {
            id:31371,
            name: '开抢中',
            page: 1,
            list: [],
          },
          {
            id:31370,
            name: '爆款预热',
            page: 1,
            list: [],
          }
        ],
        info: {
          pict_url: '//img.alicdn.com/bao/uploaded/i3/2879404129/O1CN01t9zBiK1gN7ySqBiDi_!!0-item_pic.jpg',
          title: '北极绒男士保暖裤加厚加绒裤秋裤毛裤紧身线裤打底裤单件棉裤冬季',
          sub_title: '加绒加厚羊毛护膝贴片',
          coupon_amount: 3,
          zk_final_price: '19.9',
          volume: 114057,
          user_type: 1,
        }
      }
  },
  methods: {
    // 搜索框 获取值
    onEnterSearch() {
        console.log("search: "+ this.keyword);
    },
    clickTab(key){
      this.loadingTxt = '加载中...';
      this.tabIndex = key;
      if(!this.tabs[key].list.length) this.getGoods();
    },
    getGoods(){
      if(this.loading) return;
      this.loading = true;
      var _this = this;
      let key = this.tabIndex;
      let page = this.tabs[key].page;
      let material_id = this.tabs[key].id;
      axios.get('/taobao/dGmaterialOptimus?page=' + page + '&material_id=' + material_id).then(function(res){
        _this.loading = false;
        if(res.data.code !== 0){
          _this.loadingTxt = res.data.message;
          return ;
        }

        _this.tabs[key].list = _this.tabs[key].list.concat(res.data.data);
        _this.tabs[key].page++;
        _this.tabIndex = key;
      });
    },
    setRem() {
      var docEl = document.documentElement;
      var clientWidth = docEl.clientWidth;
      if (!clientWidth) {
          return;
      }
      docEl.style.fontSize = 145 * (clientWidth / 1080) + 'px';
    },
    getbottom(){
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
      let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
      let numHeight = scrollTop + clientHeight;
      if( (scrollHeight > clientHeight) && (numHeight > scrollHeight - 30) && !this.loading){
          this.getGoods();
      }
    }
  },
  created() {
    this.getGoods();
  },
  beforeMount(){
    console.log(1)
    this.setRem();
  },
  mounted(){
      window.addEventListener('scroll', this.getbottom);
  },
  destroyed() { 
      window.removeEventListener('scroll', this.getbottom);
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.container {
    height: 100%;
    background-color: rgba(253, 51, 21, 0.88);
}
.search {
  display: block;
  margin-bottom: 10px;
}
.loading{
    font-size: .3rem;
    color: white;
    padding-top: .1rem;
    padding-bottom: .1rem;
    margin: 0;
}
    .c_fcopyright {
        font-size: 14px;
        color: #bcbcbc;
        margin-top: 16px;
        text-align: center;
    }
    .c_fcopyright a {
        font-size: 12px;
        color: #a4a4a4;
        text-decoration: none;
    }
    .lego1 {height: 2.92rem;}
    .mv-banner-comp {
    position: relative;
    /* width: 7.5rem */
    margin: 0 auto .24rem;
    background: no-repeat 50% 0;
    background-size: 100% auto;
}


.mv-tabview-boom-comp .tabview-tab {
    height: .8rem;
}
.tabview-tab {
    height: .8rem;
    margin-bottom: .24rem;
}
.xtab-padding {
    margin: 0 auto;
    border-radius: .12rem;
    width: 7.02rem;
}
.xtab {
    position: relative;
    width: 100%;
    color: #4a4a4a;
    font-size: .26rem;
    background-color: #fff;
    text-align: center;
    z-index: 990;
    box-sizing: border-box;
    box-shadow: 0 0.01rem 0 0 rgba(0,0,0,.1);
}
.xtab-padding .xtab-container {
    border-radius: .12rem;
}
.xtab-container {
    position: relative;
    background-color: inherit;
    overflow: hidden;
    width: 100%;
    height: .8rem;
    box-sizing: border-box;
}
.xtab-wrap {
    display: -webkit-box;
    display: -moz-box;
    display: box;
    display: -moz-flex;
    display: flex;
    height: 100%;
}
.xtab-container .xs-item {
    width: auto;
}
.xtab .flex-1 {
    box-flex: 1;
    -moz-flex: 1;
    flex: 1;
}
.xs-item-active {
    color: #fb3434;
}
.xtab-item-content {
    position: relative;
    width: auto;
    height: 100%;
    background-repeat: no-repeat;
    display: -webkit-inline-box;
    display: -moz-inline-box;
    display: inline-box;
    display: -moz-inline-flex;
    display: inline-flex;
    box-orient: vertical;
    box-direction: normal;
    -moz-flex-direction: column;
    flex-direction: column;
    box-pack: center;
    -moz-justify-content: center;
    justify-content: center;
    background-size: 100% 100%;
    box-sizing: border-box;
}
.xtab-icon-title {
    position: relative;
    text-align: center;
    max-width: 1.6rem;
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>